package rullekake.entities;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Size;

/**
 * Institusjonsklasse.
 * Holder på alle data og relasjonsinformasjon for institusjoner. Består av set/get for alt + equals, hashcode og toString.
 *
 */
@Entity
@Table(name = "INSTITUTION")
@NamedQueries({
    @NamedQuery(name = "Institution.findAll", query = "SELECT i FROM Institution i"),
    @NamedQuery(name = "Institution.findByInstitutionId", query = "SELECT i FROM Institution i WHERE i.institutionId = :institutionId"),
    @NamedQuery(name = "Institution.findByInstitutionName", query = "SELECT i FROM Institution i WHERE i.institutionName = :institutionName"),
    @NamedQuery(name = "Institution.findByInstitutionNameNo", query = "SELECT i FROM Institution i WHERE i.institutionNameNo = :institutionNameNo"),
    @NamedQuery(name = "Institution.findByPhoneNumber", query = "SELECT i FROM Institution i WHERE i.phoneNumber = :phoneNumber"),
    @NamedQuery(name = "Institution.findByAddress", query = "SELECT i FROM Institution i WHERE i.address = :address")})
public class Institution implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "INSTITUTION_ID")
    private Integer institutionId;
    @Basic(optional = false)
    @Column(name = "INSTITUTION_NAME")
    @Size(min = 2)
    private String institutionName;
    @Basic(optional = false)
    @Column(name = "INSTITUTION_NAME_NO")
    private String institutionNameNo;
    @Basic(optional = false)
    @Column(name = "PHONE_NUMBER")
    private String phoneNumber;
    @Basic(optional = false)
    @Column(name = "ADDRESS")
    private String address;
    @JoinTable(name = "PERSON_INSTITUTION", 
        joinColumns = { @JoinColumn(name = "INSTITUTION_ID", referencedColumnName = "INSTITUTION_ID")},
        inverseJoinColumns = { @JoinColumn(name = "PERSON_ID", referencedColumnName = "PERSON_ID")})
    @ManyToMany //(cascade = { CascadeType.ALL })
    private Collection<Person> personCollection;
    @OneToMany(mappedBy = "institution")
    private Collection<Faculty> facultyCollection;

    public Institution() {
    }

    public Institution(Integer institutionId) {
        this.institutionId = institutionId;
    }

    public Institution(Integer institutionId, String institutionName, String institutionNameNo, String phoneNumber, String address) {
        this.institutionId = institutionId;
        this.institutionName = institutionName;
        this.institutionNameNo = institutionNameNo;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }

    public Integer getInstitutionId() {
        return institutionId;
    }

    public void setInstitutionId(Integer institutionId) {
        this.institutionId = institutionId;
    }

    public String getInstitutionName() {
        return institutionName;
    }

    public void setInstitutionName(String institutionName) {
        this.institutionName = institutionName;
    }

    public String getInstitutionNameNo() {
        return institutionNameNo;
    }

    public void setInstitutionNameNo(String institutionNameNo) {
        this.institutionNameNo = institutionNameNo;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Collection<Person> getPersonCollection() {
        return personCollection;
    }

    public void setPersonCollection(Collection<Person> personCollection) {
        this.personCollection = personCollection;
    }

    public Collection<Faculty> getFacultyCollection() {
        return facultyCollection;
    }

    public void setFacultyCollection(Collection<Faculty> facultyCollection) {
        this.facultyCollection = facultyCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (institutionId != null ? institutionId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Institution)) {
            return false;
        }
        Institution other = (Institution) object;
        if ((this.institutionId == null && other.institutionId != null) || (this.institutionId != null && !this.institutionId.equals(other.institutionId))) {
            return false;

        }
        return true;
    }

    @Override
    public String toString() {
        return institutionName;
    }

}
